package com.team06.util;

import com.team06.pojo.Dfue;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DfueXmlParserUtil {
    private static final Logger LOGGER = Logger.getLogger(DfueXmlParserUtil.class.getName());

    public static Dfue parseXml(String xmlPath) {
        Dfue entity = new Dfue();
        try {
            File file = new File(xmlPath);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(file);
            Element root = document.getRootElement();

            // 解析 META 节点
            Element metaElement = root.getChild("META");
            if (metaElement != null) {
                entity.setMetaSnDR(getChildTextSafe(metaElement, "SNDR"));
                entity.setMetaRCVR(getChildTextSafe(metaElement, "RCVR"));
                String seqnText = getChildTextSafe(metaElement, "SEQN");
                if (seqnText != null && !seqnText.isEmpty()) {
                    try {
                        entity.setMetaSEQN(Integer.parseInt(seqnText));
                    } catch (NumberFormatException e) {
                        LOGGER.log(Level.WARNING, "Invalid SEQN format: " + seqnText);
                        entity.setMetaSEQN(0);
                    }
                }
                entity.setMetaDDTM(getChildTextSafe(metaElement, "DDTM"));
                entity.setMetaTYPE(getChildTextSafe(metaElement, "TYPE"));
                entity.setMetaSTYP(getChildTextSafe(metaElement, "STYP"));
            }

            // 解析 DFLT 节点
            Element dfltElement = root.getChild("DFLT");
            if (dfltElement != null) {
                // 解析所有DFLT子节点
                entity.setDfltFLID(getChildTextSafe(dfltElement, "FLID"));
                entity.setDfltAFID(getChildTextSafe(dfltElement, "AFID"));
                entity.setDfltFFID(getChildTextSafe(dfltElement, "FFID"));
                entity.setDfltFIDE(getChildTextSafe(dfltElement, "FIDE"));
                entity.setDfltAWCD(getChildTextSafe(dfltElement, "AWCD"));
                entity.setDfltFLNO(getChildTextSafe(dfltElement, "FLNO"));
                entity.setDfltFEXD(getChildTextSafe(dfltElement, "FEXD"));
                entity.setDfltFIMD(getChildTextSafe(dfltElement, "FIMD"));
                entity.setDfltFLIO(getChildTextSafe(dfltElement, "FLIO"));
                entity.setDfltFLTK(getChildTextSafe(dfltElement, "FLTK"));
                entity.setDfltFATT(getChildTextSafe(dfltElement, "FATT"));
                entity.setDfltPATT(getChildTextSafe(dfltElement, "PATT"));
                entity.setDfltMFID(getChildTextSafe(dfltElement, "MFID"));
                entity.setDfltMFFI(getChildTextSafe(dfltElement, "MFFI"));
                entity.setDfltCFTP(getChildTextSafe(dfltElement, "CFTP"));
                entity.setDfltCFNO(getChildTextSafe(dfltElement, "CFNO"));
                entity.setDfltSTAT(getChildTextSafe(dfltElement, "STAT"));
                entity.setDfltABST(getChildTextSafe(dfltElement, "ABST"));
                entity.setDfltABRS(getChildTextSafe(dfltElement, "ABRS"));
                entity.setDfltISTA(getChildTextSafe(dfltElement, "ISTA"));
                entity.setDfltIAST(getChildTextSafe(dfltElement, "IAST"));
                entity.setDfltIARS(getChildTextSafe(dfltElement, "IARS"));
                entity.setDfltMSTA(getChildTextSafe(dfltElement, "MSTA"));
                entity.setDfltMABS(getChildTextSafe(dfltElement, "MABS"));
                entity.setDfltMABR(getChildTextSafe(dfltElement, "MABR"));
                entity.setDfltMIST(getChildTextSafe(dfltElement, "MIST"));
                entity.setDfltMIAS(getChildTextSafe(dfltElement, "MIAS"));
                entity.setDfltMIAR(getChildTextSafe(dfltElement, "MIAR"));
                entity.setDfltBORT(getChildTextSafe(dfltElement, "BORT"));
                entity.setDfltMBOR(getChildTextSafe(dfltElement, "MBOR"));
                entity.setDfltTBRT(getChildTextSafe(dfltElement, "TBRT"));
                entity.setDfltMTBR(getChildTextSafe(dfltElement, "MTBR"));
                entity.setDfltLBDT(getChildTextSafe(dfltElement, "LBDT"));
                entity.setDfltMLBD(getChildTextSafe(dfltElement, "MLBD"));
                entity.setDfltPOKT(getChildTextSafe(dfltElement, "POKT"));
                entity.setDfltMPOK(getChildTextSafe(dfltElement, "MPOK"));
                entity.setDfltAPOT(getChildTextSafe(dfltElement, "APOT"));
                entity.setDfltDETT(getChildTextSafe(dfltElement, "DETT"));
                entity.setDfltDRTT(getChildTextSafe(dfltElement, "DRTT"));
                entity.setDfltDELT(getChildTextSafe(dfltElement, "DELT"));
                entity.setDfltDRLT(getChildTextSafe(dfltElement, "DRLT"));
                entity.setDfltVIP(getChildTextSafe(dfltElement, "VIP"));
                entity.setDfltPAST(getChildTextSafe(dfltElement, "PAST"));
                entity.setDfltCONT(getChildTextSafe(dfltElement, "CONT"));
                entity.setDfltPROX(getChildTextSafe(dfltElement, "PROX"));

                // 解析 AIRL 节点
                Element airlElement = dfltElement.getChild("AIRL");
                if (airlElement != null) {
                    List<Element> arptElements = airlElement.getChildren("ARPT");
                    List<Dfue.FlightSegment> segments = new ArrayList<>();

                    for (int i = 0; i < arptElements.size(); i++) {
                        Element arptElement = arptElements.get(i);
                        Dfue.FlightSegment segment = new Dfue.FlightSegment();

                        // 设置航段类型
                        if (i == 0) {
                            segment.setSegmentType("DEPARTURE");
                        } else if (i == arptElements.size() - 1) {
                            segment.setSegmentType("ARRIVAL");
                        } else {
                            segment.setSegmentType("TRANSIT");
                        }

                        // 设置其他字段
                        segment.setApno(getChildTextSafe(arptElement, "APNO"));
                        segment.setApcod(getChildTextSafe(arptElement, "APCD"));
                        segment.setFptt(getChildTextSafe(arptElement, "FPTT"));
                        segment.setFett(getChildTextSafe(arptElement, "FETT"));
                        segment.setFrtt(getChildTextSafe(arptElement, "FRTT"));
                        segment.setFplt(getChildTextSafe(arptElement, "FPLT"));
                        segment.setFelt(getChildTextSafe(arptElement, "FELT"));
                        segment.setFrlt(getChildTextSafe(arptElement, "FRLT"));
                        segment.setApat(getChildTextSafe(arptElement, "APAT"));

                        segments.add(segment);
                    }
                    entity.setFlightSegments(segments);
                }

                // 解析 GTLS 节点
                Element gtlsElement = dfltElement.getChild("GTLS");
                if (gtlsElement != null) {
                    Element gateElement = gtlsElement.getChild("GATE");
                    if (gateElement != null) {
                        Dfue.GtlsGate gtlsGate = new Dfue.GtlsGate();
                        gtlsGate.setGtno(getChildTextSafe(gateElement, "GTNO"));
                        gtlsGate.setId(getChildTextSafe(gateElement, "ID"));
                        gtlsGate.setCode(getChildTextSafe(gateElement, "CODE"));
                        gtlsGate.setGtat(getChildTextSafe(gateElement, "GTAT"));
                        gtlsGate.setEstr(getChildTextSafe(gateElement, "ESTR"));
                        gtlsGate.setEend(getChildTextSafe(gateElement, "EEND"));
                        gtlsGate.setRstr(getChildTextSafe(gateElement, "RSTR"));
                        gtlsGate.setRend(getChildTextSafe(gateElement, "REND"));
                        gtlsGate.setBtsc(getChildTextSafe(gateElement, "BTSC"));
                        entity.setGtlsGate(gtlsGate);
                    }
                }

                // 解析 STLS 节点
                Element stlsElement = dfltElement.getChild("STLS");
                if (stlsElement != null) {
                    Element stndElement = stlsElement.getChild("STND");
                    if (stndElement != null) {
                        Dfue.StlsStnd stlsStnd = new Dfue.StlsStnd();
                        stlsStnd.setStno(getChildTextSafe(stndElement, "STNO"));
                        stlsStnd.setCode(getChildTextSafe(stndElement, "CODE"));
                        stlsStnd.setEstr(getChildTextSafe(stndElement, "ESTR"));
                        stlsStnd.setEend(getChildTextSafe(stndElement, "EEND"));
                        stlsStnd.setRstr(getChildTextSafe(stndElement, "RSTR"));
                        stlsStnd.setRend(getChildTextSafe(stndElement, "REND"));
                        stlsStnd.setCssi(getChildTextSafe(stndElement, "CSSI"));
                        stlsStnd.setBtsc(getChildTextSafe(stndElement, "BTSC"));
                        entity.setStlsStnd(stlsStnd);
                    }
                }

                // 解析 TMCD 节点
                Element tmcdElement = dfltElement.getChild("TMCD");
                if (tmcdElement != null) {
                    entity.setDfltTmcdNmcd(getChildTextSafe(tmcdElement, "NMCD"));
                    entity.setDfltTmcdJmcd(getChildTextSafe(tmcdElement, "JMCD"));
                }
            }

        } catch (JDOMException | IOException | NumberFormatException e) {
            LOGGER.log(Level.SEVERE, "Failed to parse XML file: " + xmlPath, e);
            return null;
        }
        return entity;
    }

    private static String getChildTextSafe(Element parent, String childName) {
        Element child = parent.getChild(childName);
        return child != null ? child.getText() : null;
    }
}
